package com.fh.controller.app.appuser;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import org.springframework.web.context.ContextLoader;

import com.fh.controller.base.BaseController;
import com.fh.service.system.redis.RedisService;
import com.fh.service.transactionpair.TransactionPairService;
import com.fh.util.Base64;

import com.fh.util.StatusCode;

import com.fh.util.EncryptUtil;


import com.fh.util.JsonStringUtil;

import net.sf.json.JSONObject;

@ServerEndpoint(value="/exc/market/kline/{userId}")
public class WebosocketKlineController  extends BaseController  {
	   
	 private static String userId;
	 
	 private RedisService redisService = (RedisService) ContextLoader.getCurrentWebApplicationContext().getBean("redisService");
	 private TransactionPairService transactionpairService = (TransactionPairService) ContextLoader.getCurrentWebApplicationContext().getBean("transactionpairService");
	 
	 //连接时执行
	 @OnOpen
	 public void onOpen(@PathParam("userId") String userId,Session session) throws IOException{
//	     String userIdredis=new String(EncryptUtil.AESDncode(userId));
//	     if(redisService.exists(userIdredis)){
	     	logBefore(logger, "新连接：{}"+userId);
//	     }else{
	     	onClose();
//	     }
	     
	 }
	 
	 //关闭时执行
	 @OnClose
	 public void onClose(){
	 	 logBefore(logger, "连接：{} 关闭"+userId);
	 }
	 
	 		
	 //收到消息时执行
	 @OnMessage
	 public void onMessage(String message, Session session)  {
	 	logBefore(logger, "收到用户{}的消息{}"+userId+","+message);
	 	Object result ="";
	 	try{
	 		Map<String,Object> map = new  HashMap<>();
	 		String basemessage=new String(EncryptUtil.AESDncode(message));//解析传递过来的参数
	 		JSONObject menssageSocket= JsonStringUtil.getJSONObjectwebSoocket(basemessage);//在
//	 		if(redisService.exists(menssageSocket.getString("userId"))){
	    		 session.getBasicRemote().sendText(transactionpairService.getklineMerged(menssageSocket).toString()); //回复用户
	    		 result=JsonStringUtil.getJSONResult(StatusCode.SUCCESS,"成功",transactionpairService.getklineMerged(menssageSocket).toString());
//	    	 }else{
//				 result=JsonStringUtil.getJSONResult(StatusCode.SYSTEM_ERROR,"usrid错误","");

//	    	 }
	 	}catch(Exception e){
	 		 logger.error(e.toString(),e);
	 		 try {
				result=JsonStringUtil.getJSONResult(StatusCode.ERROR_INTERFACE,"接口校验失败","");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	 	
	 	}finally{
	 		 try {
				session.getBasicRemote().sendText(result.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	 	}
	 	 
	 	 
	 }
	 
	 //连接错误时执行
	 @OnError
	 public void onError(Session session, Throwable error){
	 	logBefore(logger, "用户id为：{}的连接发送错误"+this.userId);
	     error.printStackTrace();
	 }

}
